import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/utils/input_formatter_tools.dart';
import 'package:finance_app/src/widgets/button/normal_button.dart';
import 'package:finance_app/src/widgets/input/input_field.dart';
import 'package:finance_app/src/widgets/pop/normal_border_pop.dart';
import 'package:finance_app/src/widgets/select/normal_check_box.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import 'default_bottom_sheet.dart';

/// 添加收入来源
/// result
/// {
///       'amount': amountController.text,
///       'incomeType': incomeTypeController.text,
///     }
showAddIncomeTypeBottomSheet(BuildContext context, String amount,
    String incomeType, String wageType, int isCheckCPF, bool enableCPF) async {
  final result = await showDefaultBottomSheet(context,
      child: AddDiscountBottomSheet(
          amount: amount,
          incomeType: incomeType,
          enableCPF: enableCPF,
          isCheckCPF: isCheckCPF == 1,
          wageType: wageType),
      title: S.current.add_income_type,
      isScrollControlled: true,
      leading: amount == null
          ? null
          : DefaultBottomSheetLeadingButton(
              title: S.current.delete,
              onTap: amount == null
                  ? null
                  : () {
                      Navigator.of(context).pop(true);
                    },
            ));
  return result;
}

class AddDiscountBottomSheet extends StatefulWidget {
  final String amount;
  final String incomeType;
  final String wageType;
  final bool isCheckCPF;
  final bool enableCPF;

  const AddDiscountBottomSheet(
      {Key key,
      this.amount,
      this.incomeType,
      this.wageType = "ow",
      this.isCheckCPF,
      this.enableCPF})
      : super(key: key);

  @override
  _AddDiscountBottomSheetState createState() => _AddDiscountBottomSheetState();
}

class _AddDiscountBottomSheetState extends State<AddDiscountBottomSheet> {
  TextEditingController amountController = TextEditingController();
  FocusNode amountFocusNode = FocusNode();

  TextEditingController incomeTypeController = TextEditingController();
  FocusNode incomeTypeFocusNode = FocusNode();
  bool _isCheckCPF;
  bool isEnable = false;
  String _wageType;

  void _onAdd() {
    if (amountController.text.length == 0) {
      BaseConfig.dialogBase.showToast(
          context: context, content: S.current.please_enter_the_amount);
      return;
    }
    if (incomeTypeController.text.length == 0) {
      BaseConfig.dialogBase.showToast(
          context: context, content: S.current.please_enter_the_income_type);
      return;
    }
    Navigator.of(context).pop({
      'amount': amountController.text,
      'incomeType': incomeTypeController.text,
      'cpfPayable': _isCheckCPF == true ? 1 : 0,
      'wageType': _wageType,
    });
  }

  @override
  void initState() {
    super.initState();
    _isCheckCPF = widget.isCheckCPF ?? false;
    _wageType = widget.wageType ?? "ow";

    if (widget.amount != null) {
      amountController.text = widget.amount;
    }
    if (widget.incomeType != null) {
      incomeTypeController.text = widget.incomeType;
    }
    amountController.addListener(_handleUpdateListener);
    incomeTypeController.addListener(_handleUpdateListener);
  }

  @override
  void dispose() {
    super.dispose();
    amountController.dispose();
    amountFocusNode.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisSize: MainAxisSize.min,
      children: [
        AppSpaces.h30,
        PressedOpacityButton(
          onPressed: widget.enableCPF ? _onChangeCPFPayable : () {},
          padding: EdgeInsets.symmetric(horizontal: getDp(45)),
          child: NormalCheckBox(
            title: S.current.cpf_payable,
            isSelect: _isCheckCPF,
            isEnable: widget.enableCPF ?? false,
          ),
        ),
        AppSpaces.h30,
        _isCheckCPF
            ? Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisSize: MainAxisSize.min,
                children: [
                  Container(
                    child: NormalBorderPop<String>(
                      enable: widget.enableCPF,
                      horizontalPadding: getDp(45),
                      showShadow: true,
                      select: _wageType,
                      buildDisplayName: (e) => wageTypeDisplayName(e),
                      group: ["ow", "aw"],
                      onSelect: (String data) {
                        _wageType = data;
                      },
                    ),
                  ),
                  AppSpaces.h15,
                ],
              )
            : Container(),
        InputField(
          tip: S.current.income_type,
          controller: incomeTypeController,
          focusNode: incomeTypeFocusNode,
          enable: widget.enableCPF,
        ),
        AppSpaces.h15,
        InputField(
          tip: S.current.amount,
          controller: amountController,
          focusNode: amountFocusNode,
          keyboardType: TextInputType.numberWithOptions(decimal: true),
          inputFormatters: [
            doubleLimit2InputFormatter,
          ],
        ),
        AppSpaces.h30,
        NormalButton(
          title: S.current.add,
          onTap: isEnable ? _onAdd : null,
        ),
      ],
    );
  }

  void _handleUpdateListener() {
    if (amountController.text.length > 0 &&
        incomeTypeController.text.length > 0) {
      if (isEnable == false) {
        setState(() {
          isEnable = true;
        });
      }
    } else {
      if (isEnable == true) {
        setState(() {
          isEnable = false;
        });
      }
    }
  }

  void _onChangeCPFPayable() {
    setState(() {
      _isCheckCPF = !_isCheckCPF;
    });
  }

  String wageTypeDisplayName(String wageTypeKey) {
    switch (wageTypeKey) {
      case "ow":
        return S.current.ordinary_wage;
        break;
      case "aw":
        return S.current.additional_wage;
        break;
      default:
        return "";
    }
  }
}
